/*
  学习目标：Action可以携带参数, 参与reducer函数的计算
  💥 payload只是一个键名, 👎不建议修改其名字
*/

import { useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';

export default function App() {
  const [show, setShow] = useState(true);
  return (
    <div>
      <h1>Redux基础案例</h1>
      <button onClick={() => setShow(!show)}>点击切换卸载挂载</button>
      {show && <Son />}
    </div>
  );
}

function Son() {
  const count = useSelector((state) => state.count);
  const dispatch = useDispatch();

  return (
    <div>
      <h2>子组件</h2>
      <h2>count:{count}</h2>

      <button onClick={() => dispatch({ type: 'add', payload: 1 })}>+1</button>
      <button onClick={() => dispatch({ type: 'add', payload: 5 })}>+5</button>
      <button onClick={() => dispatch({ type: 'des', payload: 1 })}>-1</button>
      <button onClick={() => dispatch({ type: 'des', payload: 10 })}>-10</button>
    </div>
  );
}
